<?php
/**
 * Created by 无所谓已
 * User: Administrator
 * Date: 2025-08-18
 * Time: 18:36
 */

namespace plugin\csauto\app\service\downloader;

use FFMpeg\FFProbe;

class FfprobeService
{

    public static function getMediaInfo($filePath)
    {

        $ffprobe = FFProbe::create();


        // 获取格式信息 (General 部分)
        $format = $ffprobe->format($filePath);

        // 获取所有流信息
        $streams = $ffprobe->streams($filePath);
        $videoStream = $streams->videos()->first();
        $audioStreams = $streams->audios();
        $audioStream1 = $audioStreams->first();

        $mediaInfoText = '';
        // 输出 General 信息
        $mediaInfoText .= "=== General ===\n";
        $mediaInfoText .= "Complete name                       : " . basename($filePath) . "\n";
        $mediaInfoText .= "Format                              : " . $format->get('format_name') . "\n";
        $mediaInfoText .= "File size                           : " . formatBytes($format->get('size')) . "\n";
        $mediaInfoText .= "Duration                            : " . formatDuration($format->get('duration')) . "\n";
        $mediaInfoText .= "Overall bit rate                    : " . formatBitrate($format->get('bit_rate')) . "\n";
        $mediaInfoText .= "Writing application                 : " . ($format->get('tag:encoder') ?? 'N/A') . "\n";

        // 输出 Video 信息
        $mediaInfoText .= "\n=== Video ===\n";
        if ($videoStream) {
            $mediaInfoText .= "ID                                  : " . ($videoStream->get('index') + 1) . "\n";
            $mediaInfoText .= "Format                              : " . getVideoFormatName($videoStream->get('codec_name')) . "\n";
            $mediaInfoText .= "Codec ID                            : " . $videoStream->get('codec_tag_string') . "\n";
            $mediaInfoText .= "Duration                            : " . formatDuration($format->get('duration')) . "\n";
            $mediaInfoText .= "Width                               : " . number_format($videoStream->get('width')) . " pixels\n";
            $mediaInfoText .= "Height                              : " . number_format($videoStream->get('height')) . " pixels\n";

            // 计算显示比例
            $width = $videoStream->get('width');
            $height = $videoStream->get('height');
            $aspectRatio = calculateAspectRatio($width, $height);
            $mediaInfoText .= "Display aspect ratio                : " . $aspectRatio . "\n";

            $mediaInfoText .= "Frame rate                          : " . $videoStream->get('avg_frame_rate') . " FPS\n";
            $mediaInfoText .= "Color space                         : " . ($videoStream->get('color_space') ?? 'N/A') . "\n";
            $mediaInfoText .= "Chroma subsampling                  : " . ($videoStream->get('chroma_sample_position') ?? 'N/A') . "\n";
            $mediaInfoText .= "Bit depth                           : " . ($videoStream->get('bits_per_raw_sample') ?? 'N/A') . " bits\n";
            $mediaInfoText .= "Writing library                     : " . ($videoStream->get('tag:encoder') ?? 'N/A') . "\n";
        }

        // 输出 Audio 信息
        $mediaInfoText .= "\n=== Audio #1 ===\n";
        if ($audioStream1) {
            $mediaInfoText .= "ID                                  : " . ($audioStream1->get('index') + 1) . "\n";
            $mediaInfoText .= "Format                              : " . getAudioFormatName($audioStream1->get('codec_name')) . "\n";
            $mediaInfoText .= "Codec ID                            : " . $audioStream1->get('codec_tag_string') . "\n";
            $mediaInfoText .= "Duration                            : " . formatDuration($format->get('duration')) . "\n";
            $mediaInfoText .= "Channel(s)                          : " . $audioStream1->get('channels') . " channels\n";
            $mediaInfoText .= "Sampling rate                       : " . ($audioStream1->get('sample_rate') / 1000) . " kHz\n";
            $mediaInfoText .= "Default                             : " . (isset($audioStream1->get('disposition')['default']) && $audioStream1->get('disposition')['default'] ? 'Yes' : 'No') . "\n";
            $mediaInfoText .= "Forced                              : " . (isset($audioStream1->get('disposition')['forced']) && $audioStream1->get('disposition')['forced'] ? 'Yes' : 'No') . "\n";
        }
        return $mediaInfoText;

    }


}